package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.gulimall.coupon.entity.SmsMemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SmsSkuLadderEntity;
import com.atguigu.gulimall.coupon.service.SmsMemberPriceService;
import com.atguigu.gulimall.coupon.service.SmsSkuLadderService;
import com.atguigu.to.MemberPrice;
import com.atguigu.to.SkuReductionTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SmsSkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SmsSkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SmsSkuFullReductionService;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {

    @Autowired
    SmsSkuLadderService smsSkuLadderService;


    @Autowired
    SmsSkuFullReductionService smsSkuFullReductionService;

    @Autowired
    SmsMemberPriceService smsMemberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo SkuReductionTo) {
        //5.4）、sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //存储商品阶梯价格表
        SmsSkuLadderEntity smsSkuLadderEntity = new SmsSkuLadderEntity();
        BeanUtils.copyProperties(SkuReductionTo,smsSkuLadderEntity);
        Integer fullCount = smsSkuLadderEntity.getFullCount();
        BigDecimal disCount = smsSkuLadderEntity.getDiscount();
        BigDecimal price = SkuReductionTo.getPrice();
        BigDecimal ladderPrice = price.multiply(BigDecimal.valueOf(fullCount));
        BigDecimal prices = ladderPrice.multiply(disCount);
        smsSkuLadderEntity.setPrice(prices);
        smsSkuLadderEntity.setAddOther(SkuReductionTo.getPriceStatus());

        smsSkuLadderService.save(smsSkuLadderEntity);

        //存储商品满减信息表信息
        SmsSkuFullReductionEntity smsSkuFullReductionEntity = new SmsSkuFullReductionEntity();
        BeanUtils.copyProperties(SkuReductionTo,smsSkuFullReductionEntity);
        smsSkuFullReductionEntity.setAddOther(SkuReductionTo.getPriceStatus());
        smsSkuFullReductionService.save(smsSkuFullReductionEntity);


        //存储商品会员价格表信息
        List<MemberPrice> memberPrice = SkuReductionTo.getMemberPrice();
        List<SmsMemberPriceEntity> collect = memberPrice.stream().map(member -> {
            SmsMemberPriceEntity smsMemberPriceEntity = new SmsMemberPriceEntity();
            smsMemberPriceEntity.setSkuId(SkuReductionTo.getSkuId());
            smsMemberPriceEntity.setMemberLevelId(member.getId());
            smsMemberPriceEntity.setMemberLevelName(member.getName());
            smsMemberPriceEntity.setMemberPrice(member.getPrice());
            return smsMemberPriceEntity;

        }).collect(Collectors.toList());

        smsMemberPriceService.saveBatch(collect);



    }

}